package com.edu.dgut.controller;

import com.edu.dgut.dao.classificationRepository;
import com.edu.dgut.entity.classificationEntity;
import com.edu.dgut.service.classificationService;
import com.edu.dgut.service.mapObjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/category/")
public class classificationController {

    @Autowired
    private classificationRepository classification;

    @Autowired
    private classificationService classify;

    private mapObjectService mapObject = new mapObjectService();

    //查询所有类别
    @RequestMapping("getAllclassification")
    @ResponseBody
    public Map<String, Object> findAll(){
        List<classificationEntity> list = new ArrayList<>();
        list = classification.findAll();
        return mapObject.put(list);
    }

    //通过分类名查找分类信息
    @RequestMapping("getByCatgory")
    @ResponseBody
    public Map<String,Object> getByCatgory(String category) {
        List<classificationEntity> list = classification.findByCategoryLike("%"+category+"%");
        return mapObject.put(list);
    }

    //通过分类名新建分类
    @RequestMapping("newClassification")
    @ResponseBody
    public Map<String,Object> newClassification(String category) {
        return classify.newcategory(category);
    }

    //添加或修改分类信息,修改时一次修改一个商品的分类
    @RequestMapping("modifyClassification")
    @ResponseBody
    public Map<String,Object> saveOrModifyClassification(@RequestBody classificationEntity new_classification) {
        return classify.saveOrModify(new_classification);
    }

    //通过分类名删除整个分类信息
    @RequestMapping("deleteByCategory")
    @ResponseBody
    public Map<String, Object> deleteByCategory(String category) {
        int i = classification.deleteByCategory(category);
        Map<String,Object> map = new HashMap<String,Object>();
        if(i >= 1) {
            map.put("status", 1);
        }else {
            map.put("status", 0);
        }
        return map;
    }

    //删除一个商品的类别信息
    @RequestMapping("deleteByCategoryAndGoods")
    @ResponseBody
    public Map<String,Object> deleteByCategoryAndGoods(@RequestBody classificationEntity new_classification) {
        return classify.delete(new_classification);
    }

}
